﻿using Enterprise_Set.API.Application.Commands.Week1.Process_CRUD;
using Enterprise_Set.API.Application.Commands.Week2.Mainplan_CRUD;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Controllers.Week2
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainPlanController : ControllerBase
    {
        public MainPlanController(IMediator mediator, ILogger<MainPlanController> logger, IWebHostEnvironment webHostEnvironment)
        {
            this.mediator = mediator;
            Logger = logger;
            WebHostEnvironment = webHostEnvironment;
        }

        public IMediator mediator { get; set; }
        public ILogger<MainPlanController> Logger { get; set; }
        public IWebHostEnvironment WebHostEnvironment { get; set; }
        /// <summary>
        /// 获取物料表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> GetItemInfoData()
        {
            try
            {
                Logger.LogInformation("获取物料表数据成功");
                return await mediator.Send(new GetItemInfoDataCommand());
            }
            catch (Exception ex)
            {
                Logger.LogError("获取物料表数据失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加主生产任务
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> AddMainplanData(AddMainplanCommand cmd)
        {
            try
            {
                Logger.LogInformation("添加主生产任务成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                Logger.LogError("添加主生产任务失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示主生产任务表数据
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ShowMainplanData(ShowMainplanCommand cmd)
        {
            try
            {
                Logger.LogInformation("显示主生产任务成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                Logger.LogError("显示主生产任务失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 删除主生产计划
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> DelMainplanData(DelMainplanCommand cmd)
        {
            try
            {
                Logger.LogInformation("删除主生产任务成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                Logger.LogError("删除主生产任务失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取需要修改的主生产计划数据
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> GetMainplanData(GetMainplanCommand cmd)
        {
            try
            {
                Logger.LogInformation("获取需要修改的主生产任务成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                Logger.LogError("获取需要修改的主生产任务失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改主生产计划数据
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> UpMainplanData(UpMainplanCommand cmd)
        {
            try
            {
                Logger.LogInformation("修改主生产计划数据成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                Logger.LogError("修改主生产计划数据失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改主生产计划明细数据
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> UpMainplandetailData(UpMainplandetailCommand cmd)
        {
            try
            {
                Logger.LogInformation("修改主生产计划明细数据成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                Logger.LogError("修改主生产计划明细数据失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 导入主生产计划表数据
        /// </summary>
        /// <param name = "cmd" ></ param >
        /// < returns ></ returns >
        [HttpPost]
        public async Task<Result> AddMainplanExcelData(AddMainplanExcelCommand cmd)
        {
            try
            {
                Logger.LogInformation("修改主生产计划明细数据成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                Logger.LogError("修改主生产计划明细数据失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 导入Excel主生产计划数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> AddExcelData(IFormFile file)
        {
            //获取根目录
            var rootpath = WebHostEnvironment.WebRootPath + "/Excel/";
            //获取文件名
            var fileName = Guid.NewGuid().ToString() + file.FileName;
            using (FileStream fs = System.IO.File.Create(rootpath + fileName))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            //实例化
            AddMainplanExcelCommand addMainplanExcelCommand = new AddMainplanExcelCommand()
            { 
                ExcelName = fileName,
            };
            return await AddMainplanExcelData(addMainplanExcelCommand);
        }
    }
}
